core.form = {};

core.form.UIControl = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.UIControl', 'core.Box');

core.form.UIControl.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);
    if(aParams.id)
        this.id = aParams.id;
    this.skipFocus = aParams.skipFocus || false;
}

core.form.UIControl.prototype.setValue = function(aValue){

};

core.form.UIControl.prototype.getValue = function(){
    return null;
};

core.form.UIControl.prototype.focus = function(){
	this.fire('focus');
}

core.form.UIControl.prototype.unfocus = function(){
	this.fire('unfocus');
}

core.form.UIControl.prototype.doFocus = function(){
	return true;
}

core.form.UIControl.prototype.setDisabled = function(aDisabled){
	this.disabled = aDisabled;
	return true;
}

core.form.UIControl.prototype.doUnFocus = function(){
	
}

core.form.UIControl.prototype.doKeyPress = function(){
	return true;
}

core.Panel = function(aParams){
	this.init(aParams);
}

core.extendClass('core.Panel', 'core.Box');

core.Panel.prototype.init = function(aParams){
	var instance = this;
    this.timeout = aParams.timeout || 30000;
	this.scrollableMode = aParams.scrollable;
	if(this.scrollableMode){
		this.minWidth = 10;
		this.minHeight = 10;
	}
	this.controls = [];
	this.focused = false;
	this.on('childAdded', function(i, e, child){
		var setUpHandlers = function(child){
			instance.controls.push(child);
			child.on('focus', function(i){
				instance.focused = true;
                if(core.desktop){
  //                  core.log('Set new panel for keys, controls: '+instance.controls.length);
                    if(core.desktop.setFocusedPanel(instance)){

                    }
                }
                var controlChanged = i!=instance.currentControl;
				if(instance.currentControl && controlChanged){
					instance.currentControl.doUnFocus();
				}
				instance.currentControl = i;
                instance.fire('focus');
                if(!controlChanged)
                    return true;
				i.doFocus();
//                core.log('Focused panel:', instance.instanceID);
                return true;
			});
            child.on('change', function(i){
                instance.formChanged();
                return false;
            });
            child.on('body_change', function(aItem){
                instance.fire('body_change', null, aItem);
            })
		}
//		core.log('Added child to '+this.instanceID+', '+child.className, child.instanceOf('core.form.UIControl'));
		if(child.instanceOf('core.form.UIControl')){
			setUpHandlers(child);
		}
		child.collectChildren('core.form.UIControl', setUpHandlers);
	});
	this.superCall('init', arguments, aParams);
}

core.Panel.prototype.focus = function(){
    if(this.defaultControl){
        this.defaultControl.doFocus();
        return true;
    }
    if(this.focused){
        return false;
    }
    if(this.currentControl){
        this.currentControl.doFocus();
        return true;
    }
    if(this.controls.length>0)
        this.controls[0].doFocus();
    return true;
};

core.Panel.prototype.timeoutFinished = function(){
    this.timeoutID = null;
    if(this.changed)
        this.formChanged();
};

core.Panel.prototype.formChanged = function(){
    this.changed = true;
    if(!this.timeoutID){
        this.fire('change_by_timeout');
        this.changed = false;
        this.timeoutID = core.setTimeout(this, this.timeoutFinished, this.timeout);
    }
};

core.Panel.prototype.unfocus = function(){
    if(this.currentControl){
        this.currentControl.doUnFocus();
    }
//    core.log('Panel', this.instanceID,'unfocus');
    this.focused = false;
    this.fire('unfocus');
}

core.Panel.prototype.setWidth = function(aWidth){
	this.superCall('setWidth', arguments, aWidth);
	if(this.scrollableMode)
		this.width = aWidth;
}

core.Panel.prototype.getMinWidth = function(){
	var minWidth = this.superCall('getMinWidth', arguments);
	if(this.scrollableMode){
		this.bodyMinWidth = minWidth;
		this.minWidth = 10;
	}
	return this.minWidth;
}

core.Panel.prototype.setValue = function(aValue){
    for (var c in this.controls) {
        var control = this.controls[c];
        if(control && control.id)
            control.setValue(aValue[control.id]);
    }
};

core.Panel.prototype.getValue = function(){
    var res = {};
    for (var c in this.controls) {
        var control = this.controls[c];
        if(control && control.id)
            res[control.id] = control.getValue();
    }
    return res;
};

core.Panel.prototype.setHeight = function(aHeight){
	this.superCall('setHeight', arguments, aHeight);
	if(this.scrollableMode)
		this.height = aHeight;
}

core.Panel.prototype.getMinHeight = function(){
	var minHeight = this.superCall('getMinHeight', arguments);
	if(this.scrollableMode){
		this.bodyMinHeight = minHeight;
		this.minHeight = 10;
	}
	return this.minHeight;
}

core.Panel.prototype.resize = function(aX, aY, aWidth, aHeight){
//	core.log('panel resize', aWidth, aHeight, this.minWidth, this.minHeight,
//            this.width, this.height);
    var scrollTop = $(this.div).scrollTop();
    var scrollLeft = $(this.div).scrollLeft();
	if(this.scrollableMode)
		$(this.div).removeClass('scrollable');
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	
	if(this.scrollableMode){
		$(this.div).addClass('scrollable');
        $(this.div).scrollTop(scrollTop).scrollLeft(scrollLeft);
    }
//	$(this.div).width(aWidth);
//	$(this.div).height(aHeight);
}

core.Panel.prototype.processKeyDown = function(e){
//    core.log('processKeyDown', this.focused, e.which, e.ctrlKey, this.currentControl, this.instanceID);
	if(!this.focused || (core.desktop && core.desktop.focusedPanel!=this))
		return true;
//	if(!this.superCall('processKeyDown', arguments, e))
//		return false;
	if(e.which==9 && this.focused && !e.ctrlKey){
		if(this.controls.length==0)
			return true;
		var index = 0;
		for(var i = 0; i<this.controls.length; i++){
			if(this.controls[i]==this.currentControl){
				index = i;
				break;
			}
		}
		var direction = e.shiftKey?-1:1;
		var newIndex = index;
		newIndex += direction;
		if(newIndex<0)
			newIndex = this.controls.length-1;
		if(newIndex>=this.controls.length)
			newIndex = 0;
		this.currentControl.doUnFocus();	
		this.currentControl = this.controls[newIndex];
		this.currentControl.doFocus();
		return false;
	}else{
		if(this.currentControl)
			return this.currentControl.doKeyPress(e);
	}
	return true;
}

core.form.TextField = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.TextField', 'core.form.UIControl');

core.form.TextField.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);
	
	this.control = document.createElement('input');
	this.control.type = 'text';
	$(this.control).addClass('input_field');
    if(aParams.password)
        $(this.control).attr('type', 'password');
	this.div.appendChild(this.control);
	this.minHeight = 24;
	this.minWidth = 25;
	var inst = this;
	$(this.control).blur(function(e){
        $(inst.control).removeClass('input_selected');
		return inst.fire('unfocus', e);
	});
	$(this.control).focus(function(e){
        $(inst.control).addClass('input_selected');
		return inst.fire('focus', e);
	});
	$(this.control).keydown(function(e){
		return inst.fire('keydown', e);
	}).keyup(function(e){
        return inst.fire('change', e);
    });

}

core.form.TextField.prototype.setValue = function(aValue){
    $(this.control).val(aValue);
};

core.form.TextField.prototype.getValue = function(){
    return $(this.control).val();
};

core.form.TextField.prototype.doFocus = function(){
    $(this.control).addClass('input_selected');
	this.control.focus();
	return false;
}

core.form.TextField.prototype.doUnFocus = function(){
//#ifdef DESKTOP
	if(!jQuery.browser.msie)
		this.control.blur();
//#endif
    $(this.control).removeClass('input_selected');
	return true;
}


core.form.TextField.prototype.makeResize = function(aX, aY, aWidth, aHeight){
	$(this.control).width($(this.div).innerWidth());
	this.alignBlock(this.control, $(this.div).innerWidth(), $(this.div).innerHeight());
}

core.form.ComboBox = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.ComboBox', 'core.form.UIControl');

core.form.ComboBox.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);

	this.control = document.createElement('select');
//	this.control.type = 'text';
	$(this.control).addClass('input_field');
	this.div.appendChild(this.control);
	this.minHeight = 24;
	this.minWidth = 25;
	var inst = this;
	$(this.control).blur(function(e){
        $(inst.control).removeClass('input_selected');
		return inst.fire('unfocus', e);
	});
	$(this.control).focus(function(e){
        $(inst.control).addClass('input_selected');
		return inst.fire('focus', e);
	});
	$(this.control).keydown(function(e){
		return inst.fire('keydown', e);
	}).keyup(function(e){
        return inst.fire('change', e);
    });
    var opts = $.makeArray(aParams.options);
    for (var i = 0; i < opts.length; i++) {
        var opt = opts[i];
        var option = core.newElement(this.control, 'option');
        $(option).attr('value', opt.id).text(opt.name);
    }
}

core.form.ComboBox.prototype.setValue = function(aValue){
    $(this.control).val(aValue);
};

core.form.ComboBox.prototype.getValue = function(){
    return $(this.control).val();
};

core.form.ComboBox.prototype.doFocus = function(){
    $(this.control).addClass('input_selected');
	this.control.focus();
	return false;
}

core.form.ComboBox.prototype.doUnFocus = function(){
//#ifdef DESKTOP
	if(!jQuery.browser.msie)
		this.control.blur();
//#endif
    $(this.control).removeClass('input_selected');
	return true;
}


core.form.ComboBox.prototype.makeResize = function(aX, aY, aWidth, aHeight){
	$(this.control).width($(this.div).innerWidth());
	this.alignBlock(this.control, $(this.div).innerWidth(), $(this.div).innerHeight());
}

core.form.TextArea = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.TextArea', 'core.form.UIControl');

core.form.TextArea.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);
	
	this.control = document.createElement('textarea');
	$(this.control).addClass('input_area');
    if(aParams.cls){
        $(this.control).addClass(aParams.cls);
    }
	this.div.appendChild(this.control);
	this.minHeight = aParams.minHeight || 20;
	this.minWidth = 25;
	var inst = this;
	
	$(this.control).blur(function(e){
        $(inst.control).removeClass('input_selected');
		inst.fire('unfocus', e);
	});
	$(this.control).focus(function(e){
        $(inst.control).addClass('input_selected');
		inst.fire('focus', e);
	});
	$(this.control).keydown(function(e){
		return inst.fire('keydown', e);
	}).keyup(function(e){
        return inst.fire('change', e);
    });
}

core.form.TextArea.prototype.doFocus = function(){
    $(this.control).addClass('input_selected');
	this.control.focus();
	return false;
}

core.form.TextArea.prototype.doUnFocus = function(){
//#ifdef DESKTOP
	if(!jQuery.browser.msie)
		this.control.blur();
//#endif
    $(this.control).removeClass('input_selected');
	return true;
}

core.form.TextArea.prototype.setValue = function(aValue){
    $(this.control).val(aValue);
    this.fire('change');
};

core.form.TextArea.prototype.getValue = function(){
    return $(this.control).val() || '';
};

core.form.TextArea.prototype.makeResize = function(aX, aY, aWidth, aHeight){
	$(this.control).width($(this.div).innerWidth());
	$(this.control).height($(this.div).innerHeight()-2);
//	this.alignBlock(this.control, $(this.div).innerWidth(), $(this.div).innerHeight());
}

core.form.CheckBox = function(config){
    this.init(config);
};

core.extendClass('core.form.CheckBox', 'core.form.UIControl');

core.form.CheckBox.prototype.init = function(config){
    this.superCall('init', arguments, config);
    this.align = config.align || 'left';
    this.checked = config.checked || false;
    this.text = config.text || '';
    this.icons = config.icons || [];
    this.content = core.newDiv(this.div, '');
    this.chImg = core.newElement(this.content, 'img', 'icon checkbox_icn');
    this.chImg.src = 'res/core/sp.png';
    this.contentSpan = core.newElement(this.content, 'span', '');
//    $(this.content).append(this.chImg);
    $(this.contentSpan).html(this.imagesToHTMLSprite(this.icons)+'&nbsp;'+this.text);
    this.focused = false;
    this.setImgClass();
    var instance = this;
    $(this.div).addClass('checkbox').mousedown(function(e){
        if(e.which==1){
            instance.fire('focus');
            instance.setValue(!instance.checked);
            return false;
        }
        return true;
    });
};

core.form.CheckBox.prototype.setWidth = function(aWidth){
	this.width = aWidth;
	$(this.div).width(aWidth);
//	$(this.textDiv).html(this.text);
	this.minHeight = $(this.content).outerHeight();
};

core.form.CheckBox.prototype.setImgClass = function(){
    $(this.chImg).attr('className', 'icon checkbox_icn '+(this.focused?
        (this.checked?'checkbox_icn_on_sel':'checkbox_icn_off_sel'):
        (this.checked? 'checkbox_icn_on':'checkbox_icn_off')));
}

core.form.CheckBox.prototype.makeResize = function(aX, aY, aWidth, aHeight){
    this.alignElement(this.content, aWidth, aHeight, this.align, 'top');
};

core.form.CheckBox.prototype.setValue = function(aValue, aForceChange){
    var changed = this.checked!=aValue;
    this.checked = aValue;
    this.setImgClass();
    if(changed || aForceChange)
        this.fire('change', null, this.checked);
};

core.form.CheckBox.prototype.getValue = function(){
    return this.checked;
};

core.form.CheckBox.prototype.doFocus = function(){
    this.focused = true;
    this.setImgClass();
	return true;
};

core.form.CheckBox.prototype.setDisabled = function(aDisabled){
	this.disabled = aDisabled;
	return true;
};

core.form.CheckBox.prototype.doUnFocus = function(){
    this.focused = false;
    this.setImgClass();
};

core.form.CheckBox.prototype.doKeyPress = function(e){
    if(e.which==32 || e.which==10 || e.which==13){
        this.setValue(!this.checked);
        return false;
    }
	return true;
};

core.ui = {};

core.ui.createRightButtons = function(config){
    return new core.Box({
        colspan: config.colspan || 1,
        columns: '1, d',
        items:[
            new core.form.Button({
                text: config.okCaption || 'Save',
                handler: config.ok,
                align: 'right'
            }),
            new core.form.Button({
                text: config.cancelCaption || 'Cancel',
                handler: config.cancel
            })
        ]
    });
}